package com.changgou.goods.Controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author caixiaofeng
 * @company xxx公司科技有限公司
 * @create 2020-04-28 11:25
 */
@RestController
@CrossOrigin
public class BrandControllerw {
    @Resource
    private BrandService brandService;


    @RequestMapping(value = {"/brands"}, method = RequestMethod.GET)
    public Result<Brand> findAll() {
        List<Brand> brandList = brandService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    @RequestMapping(value = {"/{id}"}, method = RequestMethod.GET)
    public Result<Brand> findById(@PathVariable("id") Integer id) {
        Brand brand = brandService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", brand);
    }

    @RequestMapping(value = {"/brand"}, method = RequestMethod.POST)
    public Result<Brand> add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result<>(true, StatusCode.OK, "添加成功");
    }

    @RequestMapping(value = {"/{id}"}, method = RequestMethod.PUT)
    public Result<Brand> update(@RequestBody Brand brand, @PathVariable("id") Integer id) {
        brand.setId(id);
        brandService.update(brand);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(value = {"/{id}"}, method = RequestMethod.DELETE)
    public Result<Brand> delete(@PathVariable("id") Integer id) {
        brandService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }

    @RequestMapping(value = {"/search"}, method = RequestMethod.GET)
    public Result<Brand> findList(@RequestParam Map<String, Object> searchMap) {
        List<Brand> brandList = brandService.findList(searchMap);
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    @RequestMapping(value = {"/search/{page}/{size}"}, method = RequestMethod.GET)
    public Result<Brand> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<Brand> pages = brandService.findPage(page, size);
        PageResult<Brand> pageResult = new PageResult<>(pages.getTotal(), pages.getResult());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }

    @RequestMapping(value = {"/searchs/{page}/{size}"}, method = RequestMethod.GET)
    public Result<Brand> findPage(@RequestParam Map<String, Object> searchMap, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<Brand> pages = brandService.findPage(searchMap, page, size);
        PageResult<Brand> pageResult = new PageResult<>(pages.getTotal(), pages.getResult());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }

    @RequestMapping(value = {"/category/{category}"}, method = RequestMethod.GET)
    public Result<Brand>  findListByCategoryName(@PathVariable String category) {
        List<Map> listByCategoryName = brandService.findListByCategoryName(category);
        return new Result<>(true, StatusCode.OK, "查询成功", listByCategoryName);
    }

}
